package com.atguigu.headline.controller;

import com.atguigu.headline.pojo.NewsUser;
import com.atguigu.headline.pojo.vo.HeadlineDetailVo;
import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import com.atguigu.headline.pojo.NewsType;
import com.atguigu.headline.pojo.Result;
import com.atguigu.headline.service.NewsHeadlineService;
import com.atguigu.headline.service.NewsTypeService;
import com.atguigu.headline.service.impl.NewsHeadlineServiceImpl;
import com.atguigu.headline.service.impl.NewsTypeServiceImpl;
import com.atguigu.headline.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/portal/*")
public class PortalController extends BaseController{
    NewsTypeService newsTypeService = new NewsTypeServiceImpl();
    private NewsHeadlineService headlineService=new NewsHeadlineServiceImpl();
    protected void findAllTypes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<NewsType> newsType =newsTypeService.findAll();
        Result result = Result.ok(newsType);
        WebUtil.writeJson(resp,result);
    }


    protected void findNewsPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HeadlineQueryVo headlineQueryVo = WebUtil.readJson(req, HeadlineQueryVo.class);
        Map map=headlineService.pageQuery(headlineQueryVo);
        Map finalMap=new HashMap();
        finalMap.put("pageInfo", map);
        WebUtil.writeJson(resp,Result.ok(finalMap));
    }


    protected void showHeadlineDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int hid= Integer.parseInt(req.getParameter("hid"));
        HeadlineDetailVo headlineDetailVo=headlineService.showHeadlineDetail(hid);
        Map map=new HashMap();
        map.put("headline",headlineDetailVo);
        WebUtil.writeJson(resp,Result.ok(map));
    }
}
